/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chl.niclun.projectwebshop.nbp;

import edu.chl.niclun.projectwebshop.nbp.Entities.Movie;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ActionEvent;

/**
 *Controllerbean for the RightMenu
 * @author niclas
 */
@ManagedBean(name="RightMenuControllerBean")
@RequestScoped
public class RightMenuControllerBean {
    
    @ManagedProperty(value="#{RightMenuBackingBean}")
    private RightMenuBackingBean menuBackingBean;
    
    @ManagedProperty(value="#{CartBean}")
    private CartModelBean cartModelBean;
    
    @ManagedProperty(value="#{LoginBean}")
    private LoginBean loginBean;
    
    /** Creates a new instance of menuRightControllerBean */
    public RightMenuControllerBean() {
    }
    //navigate to login
    public String navigateToLogin() {
        return "login";
    }
    //navigate to cart
    public String navigateToViewCart() {
        return "viewCart";
    }
    //navigate to index.xhtml
    public String navigateToHomePage() {
        return "index";
    }
    //update the cartpanel in the RightMenu
    @PostConstruct
    public void update() {
        menuBackingBean.setCart(cartModelBean.getCart());
        menuBackingBean.setCount(cartModelBean.getCount());
        menuBackingBean.setTotalCost(cartModelBean.getTotalCost());
        menuBackingBean.setShippingCost(cartModelBean.getShippingCost());
        menuBackingBean.setTotCostIncShipping(cartModelBean.getTotCostIncShipping());
    }
    
    public RightMenuBackingBean getMenuBackingBean() {
        return menuBackingBean;
    }
    
    public void setMenuBackingBean(RightMenuBackingBean menuBean) {
        this.menuBackingBean = menuBean;
    }
    
    public CartModelBean getCartModelBean() {
        return cartModelBean;
    }
    
    public void setCartModelBean(CartModelBean cartModelBean) {
        this.cartModelBean = cartModelBean;
    }

    public LoginBean getLoginBean() {
        return loginBean;
    }

    public void setLoginBean(LoginBean loginBean) {
        this.loginBean = loginBean;
    }
    
    
    public void removeProduct(Movie m) {
        cartModelBean.removeFromCart(m);
        update();
    }
    
    public void removeProduct(ActionEvent ae) {
        /*DataTable t = (DataTable)ae.getComponent().getParent().getParent();
        int index = t.getRowIndex();
        cartModelBean.removeFromCart(index);
        update();*/
    }
    //log out the user
    public void logOut(ActionEvent evt){
        loginBean.setIsLoggedIn(false);
        loginBean.setUser(null);
    }
    
}
